package com.paisheng.justlife.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import com.paisheng.justlife.appliction.MyApplication;
import com.paisheng.justlife.bean.ComBean;
import com.paisheng.justlife.common.SerialNumber;
import com.paisheng.justlife.utils.MyFunc;
import com.paisheng.justlife.utils.SerialPortUtils;
import com.paisheng.justlife.utils.SubSrtingUtils;

public class SerialService extends Service {

    SerialPortUtils serialPortUtils;
    String sMsg;
    Intent intent;
    boolean flag1 = false;
    boolean flag2 = false;
    boolean flag3 = false;
    boolean flag4 = false;
    boolean threadBoolean = true;
    Thread thread;
    MyBroadCastReceive receive;
    public SerialService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        intent = new Intent();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.paisheng.justlife.service.SerialService");
        receive = new MyBroadCastReceive();
        registerReceiver(receive,filter);
        MyApplication content = (MyApplication) getApplicationContext();
        serialPortUtils = content.serialPortUtils;
        this.serialPortUtils.setOnFacilityMessgerListener(new SerialPortUtils.OnFacilityMessgerListener() {
            @Override
            public void onReceive(ComBean ComRecData) {
                sMsg = MyFunc.ByteArrToHex(ComRecData.bRec);
                System.out.println(sMsg);
                if (sMsg.equals(SubSrtingUtils.getStringVerify(sMsg))) {
                    getDownDoorStatus(sMsg); //获取下仓门状态
                }

            }
        });

//        thread = new Thread(new MyThread());
//        thread.start();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true) {
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    serialPortUtils.sendSerialPort(SerialNumber.MONITORING_CLOTHES);
//                }
//            }
//        }).start();





    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        thread = new Thread(new MyThread());
        thread.start();
    }

    private void getDownDoorStatus(String sMsg) {
        String belowDoorClose = SubSrtingUtils.getBelowDoorClose(sMsg);
        String boxNumber = SubSrtingUtils.getBoxNumber(sMsg);
        if (boxNumber.equals("1") && belowDoorClose.equals("0")) {   //判断1号箱
                flag1 = true;
        }

        if (boxNumber.equals("2") && belowDoorClose.equals("0")) {   //判断2号箱
                flag2 = true;
        }

        if (boxNumber.equals("3") && belowDoorClose.equals("0")) {   //判断3号箱
                flag3 = true;
        }

        if (boxNumber.equals("4") && belowDoorClose.equals("0")) {   //判断4号箱
                flag4 = true;
        }


        if (flag1 && flag2 && flag3 && flag4) {
            intent.putExtra("flag", true);
            intent.setAction("SerialService.on-off");
            sendBroadcast(intent);
        }

    }

    class MyBroadCastReceive extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean openBoor = intent.getBooleanExtra("openBoor",false);
            flag1 = openBoor;
            flag2 = openBoor;
            flag3 = openBoor;
            flag4 = openBoor;

        }
    }


    class MyThread extends Thread{

        @Override
        public void run() {
            while (threadBoolean) {
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                SerialService.this.serialPortUtils.sendSerialPort(SerialNumber.MONITORING_PLASTIC);
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                SerialService.this.serialPortUtils.sendSerialPort(SerialNumber.MONITORING_CLOTHES);
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                SerialService.this.serialPortUtils.sendSerialPort(SerialNumber.MONITORING_METAL);
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                SerialService.this.serialPortUtils.sendSerialPort(SerialNumber.MONITORING_PAPER);
            }
         }

    }


    @Override
    public void onDestroy() {
        Log.e("service", "service:销毁------------------");
        threadBoolean = false;
        unregisterReceiver(receive);
    }
}
